<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入电影名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="movieList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电影编号" prop="id" width="120" align="center" />
      <el-table-column
        label="电影名称"
        prop="name"
        :show-overflow-tooltip="true"
        width="200"
        align="center"
      />
      <el-table-column
        label="票房(万元)"
        prop="box"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
      />
      <el-table-column label="主演" prop="main_actor" width="200" align="center" />
      <el-table-column label="首页展示" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show"
            active-value="1"
            inactive-value="0"
            @change="handleShowChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="发布状态" align="center" prop="state" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.state === '即将上映' ? 'warning' : scope.row.state === '正在热映' ? 'success' : 'danger'"
            disable-transitions
          >{{scope.row.state=='即将上映'?scope.row.date:scope.row.state}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getMovieList"
    />

    <!-- 添加或修改电影信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-row :gutter="15">
        <el-form ref="formData" :model="formData" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="Id" prop="id">
              <el-input
                v-model="formData.id"
                disabled
                placeholder="系统产生"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电影名称">
              <el-input
                v-model="formData.name"
                placeholder="请输入电影名称"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电影类型">
              <el-input
                v-model="formData.type"
                placeholder="请输入电影类型"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票房">
              <el-input
                v-model="formData.box"
                placeholder="请输入票房"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上映地区">
              <el-input
                v-model="formData.showloc"
                placeholder="请输入上映地区"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地区">
              <el-input
                v-model="formData.locate"
                placeholder="请输入地区"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评分">
              <el-input
                v-model="formData.grade"
                placeholder="请输入评分"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="想看">
              <el-input
                v-model="formData.want"
                placeholder="请输入想看"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长">
              <el-input
                v-model="formData.duration"
                placeholder="请输入时长"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上映日期">
              <el-date-picker
                v-model="formData.date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :style="{width: '100%'}"
                placeholder="请选择上映日期"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主演">
              <el-input
                v-model="formData.main_actor"
                placeholder="请输入主演(/分割)"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="语言">
              <el-input
                v-model="formData.language"
                placeholder="请输入语言"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上映状态">
              <el-select
                v-model="formData.state"
                placeholder="请选择上映状态"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in stateOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页显示">
              <el-switch v-model="formData.show" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="封面图片">
              <el-input
                v-model="formData.poster"
                placeholder="请输入封面图片"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述">
              <el-input
                v-model="formData.describe"
                type="textarea"
                placeholder="请输入描述"
                :autosize="{minRows: 4, maxRows: 4}"
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>

    <!-- 电影信息详细 -->
    <el-dialog title="电影信息详细" :visible.sync="openDetail" width="1000px" append-to-body>
      <el-form ref="Detailform" :model="Detailform" label-width="100px" size="mini">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="电影编号">{{Detailform.id}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="电影名称">{{Detailform.name}}</el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="票房(万元)">{{Detailform.box}}</el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="评分">{{Detailform.grade}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="想看">{{Detailform.want}}</el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item label="电影类型">{{Detailform.type}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区">{{Detailform.locate}}</el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="时长(分钟)">{{Detailform.duration}}</el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="上映地区">{{Detailform.showloc}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上映日期">{{Detailform.date}}</el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="主演">{{Detailform.main_actor}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">{{Detailform.state}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="语言">{{Detailform.language}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="封面图片">
              <el-popover placement="bottom" trigger="hover">
                <el-image
                  style="width: 150px; height: auto;"
                  :src="Detailform.poster"
                  fit="contain"
                ></el-image>
                <span slot="reference">{{Detailform.poster}}</span>
              </el-popover>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述">{{Detailform.describe}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMovie,
  updateShow,
  addMovie,
  getMovieById,
  updateMovie,
  deleteMovie
} from "@/api/movie/list";

export default {
  name: "List",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色表格数据
      movieList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      Detailform: {},
      // 状态数据字典
      statusOptions: [
        {
          value: "0",
          label: "正在热映"
        },
        {
          value: "1",
          label: "即将上映"
        },
        {
          value: "2",
          label: "已下映"
        }
      ],
      // 数据范围选项
      stateOptions: [
        {
          value: "正在热映",
          label: "正在热映"
        },
        {
          value: "即将上映",
          label: "即将上映"
        },
        {
          value: "已下映",
          label: "已下映"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      formData: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getMovieList();
  },
  methods: {
    /** 查询角色列表 */
    getMovieList() {
      this.loading = true;
      listMovie(this.queryParams).then(response => {
        this.movieList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 首页展示修改
    handleShowChange(row) {
      updateShow(row.id, row.show).then(res => {
        this.msgSuccess("修改成功");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        type: undefined,
        box: undefined,
        showloc: undefined,
        locate: undefined,
        grade: undefined,
        want: undefined,
        duration: undefined,
        date: null,
        main_actor: undefined,
        language: undefined,
        state: undefined,
        show: "0",
        poster: undefined,
        describe: undefined
      };
      this.resetForm("formData");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getMovieList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电影信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getMovieById(row.id).then(res => {
        this.formData = res.data;
        this.open = true;
        this.title = "修改电影信息";
      });
    },
    /** 电影详细信息 */
    handleView(row) {
      this.openDetail = true;
      this.Detailform = row;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            updateMovie(this.formData).then(res => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getMovieList();
            });
          } else {
            addMovie(this.formData).then(res => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getMovieList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return deleteMovie(Ids);
        })
        .then(() => {
          this.getMovieList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>